/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wirelesspienetwork.overviewexample.slice;

import com.wirelesspienetwork.overview.model.OverviewAdapter;
import com.wirelesspienetwork.overview.model.ViewHolder;
import com.wirelesspienetwork.overview.views.Overview;
import com.wirelesspienetwork.overviewexample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.ArrayList;
import java.security.SecureRandom;

public class MainAbilitySlice extends AbilitySlice implements Overview.RecentsViewCallbacks {

    boolean mVisible;
    // Top level views
    Overview mRecentsView;
    ArrayList<RgbColor> models;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mRecentsView = (Overview) findComponentById(ResourceTable.Id_recents_view);
        mRecentsView.setCallbacks(this);

        mVisible = true;

        models = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            SecureRandom random = new SecureRandom();
            random.setSeed(i);
            RgbColor color = new RgbColor(random.nextInt(255), random.nextInt(255), random.nextInt(255));
            models.add(color);
        }

        final OverviewAdapter stack = new OverviewAdapter<ViewHolder<Component, RgbColor>, RgbColor>(models) {
            @Override
            public ViewHolder onCreateViewHolder(Context context, ComponentContainer parent) {
                Component v = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_recents_dummy,null,false);
                return new ViewHolder<Component, Integer>(v);
            }

            @Override
            public void onBindViewHolder(ViewHolder<Component, RgbColor> viewHolder) {
                ShapeElement shapeElement = new ShapeElement();
                shapeElement.setRgbColor(viewHolder.model);
                viewHolder.itemView.setBackground(shapeElement);
            }
        };

        mRecentsView.setTaskStack(stack);
    }

    @Override
    public void onCardDismissed(int position) {
    }

    @Override
    public void onAllCardsDismissed() {
    }
}
